package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

  //  注入Dao对象
    @Autowired
    private CheckItemDao checkItemDao;

    //增加检查项
    @Override
    public void add(CheckItem checkItem) {
      checkItemDao.add(checkItem);
    }

   //分页查看检查项
  @Override
  public PageResult findPage(QueryPageBean queryPageBean) {
      //获取一页几行
    Integer pageSize = queryPageBean.getPageSize();
    //获取当前页
    Integer currentPage = queryPageBean.getCurrentPage();
    //获取查找的字符串
    String queryString = queryPageBean.getQueryString();
    //利用分页助手
    PageHelper.startPage(currentPage,pageSize);
    //调用dao层方法
    Page<CheckItem> page = checkItemDao.selectByCondition(queryString);
    long total = page.getTotal();
    List<CheckItem> rows = page.getResult();
    return new PageResult(total,rows);
  }

  //判断是否能删除检查项
  @Override
  public void deleteById(Integer id) {
      //判断当前id是否被加入检查组中
    long count = checkItemDao.findCountByCheckItemId(id);
    if (count>0){
      //当前id被加入检查组中,不能被删除
        new RuntimeException();
    }else{
      //当前id可以删除
       checkItemDao.deleteById(id);
    }

  }

  //修改
  @Override
  public void edit(CheckItem checkitem) {

      checkItemDao.edit(checkitem);
  }

  //根据 Id 查询
  @Override
  public CheckItem findById(Integer id) {

    return checkItemDao.findById(id);
  }

  @Override
  public List<CheckItem> findAll() {
    return checkItemDao.findAll();
  }

}
